
library(XML)
library(xml2)
library(RCurl)
library(rvest)
library(stringr)
library(readxl)
library(tidyverse)
library(scales)
library(lubridate)
library(showtext)
require(RSelenium)
source("./hg_rselenium_funs.R", encoding = "UTF-8")

system("java -jar C:/krupSoftware/bin/selenium-server-standalone-3.7.1.jar",
       wait = FALSE, invisible = FALSE
       )

system("java -jar C:/krupSoftware/bin/selenium-server-standalone-3.13.0.jar",
       wait = FALSE, invisible = FALSE
)

url = "https://item.jd.com/1640917.html"
url1 = "https://item.jd.com/3377132.html"
url2 = "https://item.jd.com/5657902.html"
url3 = "https://item.jd.com/6287165.html"

remdr <- remoteDriver(browserName = "phantomjs")
remdr <- remoteDriver(browserName = "firefox")
remdr
test = remdr$open()

remdr$close(test)


remdr$navigate("https://item.jd.com/3377132.html")
remdr$getTitle()
remdr$getCurrentUrl()

print(hg_rs_check_loaded(remdr))
remdr$screenshot(display = 1)
hg_rs_scroll(remdr)
remdr$screenshot(display = 1)
# remdr$close()

# 读取源代码
mypage <- remdr$getPageSource()
page <- xml2::read_html(mypage[[1]])


urltest = "https://item.jd.com/24436108263.html"

hg_rs_jd_product(page)







### 得到最终数据




### pre
source("./hg_rselenium_funs.R", encoding = "UTF-8")
remdr <- remoteDriver(browserName = "firefox")
remdr <- remoteDriver(browserName = "phantomjs")
hg_rs_get_sessions_id(remdr)
hg_rs_close(remdr)
hg_rs_open(remdr)
### 

urls = hg_rs_jd_search_n_pages_urls(remdr, "婴儿 测温计", n_pages = 2)
products = hg_rs_jd_products(remdr, "婴儿 测温器", 
                             "c:/Users/krup/Documents/temp/products-cewenqi.csv",
                             n_pages = 5, n_products = 100)
products





### test
urlhk = "https://item.jd.com/1950117961.html"

remdr$navigate(url)
remdr$navigate("https://item.jd.hk/1950117961.html")
remdr$getWindowPosition(remdr$getCurrentWindowHandle())
remdr$getWindowPosition()
remdr$getWindowSize()
remdr$getCurrentUrl()
hg_rs_get_sessions_id(remdr)
hg_rs_close(remdr)

page = hg_rs_remdr2page(remdr)
hg_rs_jd_product(page)


writedf = data.frame(stringsAsFactors = FALSE)
tdf = data.frame(a = "a", b = 2)

wrst = tdf
writedf = bind_rows(writedf, wrst)
writedf


i = 0
lapply(1:10, function(w) {
  i <<- i + 1
  print(i)
})





